/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Application;

import Maps.GoogleMaps;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.TextField;

/**
 *
 * @author Stanley
 */
public class Search extends Form implements  CommandListener{
    private MainMidlet midlet;
    private TextField address;
    // command
    private Command ok;
    private Command cancel;
    
    public Search(MainMidlet midlet) {
        super ("Search", null);
        
        address = new TextField("Location : ", null, 255, TextField.ANY);
        // append items
        append(address);
        
        this.midlet = midlet;
        // command
        ok = new Command("OK", Command.OK, 1);
        cancel = new Command("Cancel", Command.CANCEL, 1);
        setCommandListener(this);
        addCommand(ok);
        addCommand(cancel);
    }
    
    public void commandAction(Command c, Displayable d) {
        if (c == cancel) {
            midlet.homeShow();
        } else if( c == ok) {
            String location = address.getString();
            try {
                // retrieve new image map
                GoogleMaps maps  = new GoogleMaps();
                // get coordinates
                double[] lanLng = maps.geocodeAddress(location);
                // retrieve new maps
                Image map = maps.retrieveStaticImage(this.getWidth(), this.getHeight(), lanLng[0], lanLng[1], ZoomLevel.DEFAULT, "png32", midlet.getHome().getMapType());
                // set map and coordinates into home screen
                midlet.getHome().setLanLng(lanLng);
                midlet.getHome().setMap(map);
                midlet.getHome().setZoomLevel(ZoomLevel.DEFAULT);
                midlet.homeShow();
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
}
